﻿
var mysql  = require('mysql')
var http = require('http')


////////////////////////////////////////////

var showNumsPerPage_G = 50
var names_G = {}
var response_G = null

function getSqlConnection()
{
  //var connection = mysql.createConnection({host: 'rdsea0aj9n7m3g48ta5u.mysql.rds.aliyuncs.com', database: 'ad688com', user:'ad688com',password : 'ad688com',port: '3306',dateStrings: true ,}); 
  var connection = mysql.createConnection({host: 'game1715.com', database: 'test', user:'t',password : 's123',port: '3306',dateStrings: true ,}); 
  return connection
}


//对于不同数据表只需要创建不同的getDataxxx函数 然后交给getMysqlData调用就可以了
function getDataOfTest(onGet)
{
  names_G = ['test1','test2','test3']
  onGet([{test1:1,test2:2,test3:3},{test1:2,test2:112,test3:333}])
}

function getDataOfApis(onGet)
{
  names_G = ['id','name','cap','price','geo','preview']
  var ID = 1
  var DATA = []

  var connection = getSqlConnection()
  var  userGetSql = 'SELECT * FROM interface_info_v1'
  connection.query(userGetSql,function (err, result) 
  {
      if(err){
        console.log('[SELECT ERROR] - ',err.message)
        return
      }        
      for(i=0;i<result.length;i++)
      {
        var d = result[i]
        var dd = {id:ID,name:d.offer_name,cap:d.daily_cap,price:d.price,geo:d.geo,preview:d.preview_link}
        DATA[DATA.length] = dd
        ID = ID + 1
      }

      var  userGetSql = 'SELECT * FROM api_appflood'
      connection.query(userGetSql,function (err, result) 
      {
              if(err){
                console.log('[SELECT ERROR] - ',err.message)
                return
              }        
             for(i=0;i<result.length;i++)
             {
                var d = result[i]
                var dd = {id:ID,name:d.name,cap:d.cap,price:d.price,geo:d.geo,preview:d.preview}
                dd.cap = "待定"
                DATA[DATA.length] = dd        
                ID = ID + 1
             }

            var  userGetSql = 'SELECT * FROM api_glispa'
            connection.query(userGetSql,function (err, result) 
            {
                if(err){
                    console.log('[SELECT ERROR] - ',err.message)
                    return
                }        
                for(i=0;i<result.length;i++)
                {
                    var d = result[i]
                    var dd = {id:ID,name:d.name,cap:d.cap,price:d.price,geo:d.countries,preview:d.preview}
                    dd.cap = dd.cap || "待定"
                    dd.price = dd.price || "待定"  
                    dd.preview = ''
                    DATA[DATA.length] = dd
                    ID = ID + 1
                }

                var  userGetSql = 'SELECT * FROM api_isrc_campaigns'
                connection.query(userGetSql,function (err, result) 
                {
                    if(err){
                      console.log('[SELECT ERROR] - ',err.message)
                      return
                    }        
                   for(i=0;i<result.length;i++)
                   {
                      var d = result[i]
                      var dd = {id:ID,name:d.title,cap:d.cap,price:d.bid,geo:d.geoTargeting,preview:d.preview}
                      dd.cap = dd.cap || "待定"
                      dd.preview = ''
                      DATA[DATA.length] = dd
                      ID = ID + 1
                   }
                  onGet(DATA)
                })
            })
      })
  })
}

//获取数据库data 获取到数据后执行onget 将数据传入onget
function getMysqlData(onGet)
{ 
  getDataOfApis(onGet)
}

//////////////////////////////////////////////////////////////////////////
function getHtmlByJson(data, pageIndex, showNumsPerPage )
{

    showNumsPerPage = showNumsPerPage || showNumsPerPage_G
    pageIndex = pageIndex || 1
    var nameLength = names_G.length
    var dataLength = data.length
    var pageNum = Math.round(dataLength / showNumsPerPage) 

    var html =
    '<head>'+
    '<meta http-equiv="Content-Type" '+
    'content="text/html; charset=UTF-8" />'+
    '<script src="forClient.js"></script>'+ 
    '<style type="text/css">'+ 
    'td{overflow:hidden;white-space:nowrap; }'+  //将超出内容设为隐藏
    //'td{word-wrap:break-word}'+  //自动换行
    '</style>'+ 
    '</head>'+
    '<body>'+
    '<table align="center">' + 
    '<tr>'+
    '<td><button onclick="previous()">上一页</button></td>'+
    '<td><lable id = "pageIndex">' + pageIndex + '</lable></td>'+
    '<td><button onclick="next()">下一页</button></td>'+  
    '<td>共</td>'+
    '<td><lable id = "pageNum">' + pageNum +  '</lable></td>'+ 
    '<td>页</td>' +
    '<td><input type="text" id="gotoPage"></td>' + 
    '<td><button onclick="goto()">go</button></td>'+  
    '</tr>'+
    '</table>' + 
    '<table width="1000" border="1" cellspacing="0" cellpadding="10" bordercolor="#7C7C7C"'+ 
    'align = "center" style="table-layout:fixed;margin-top:10px;margin-bottom:30px;margin-left:60px">'

    //第一排是变量名字
    tdWidth = [30,200,30,30,60,250]
    html = html + '<tr>'
    for(i=0;i<nameLength;i++)
    {
      html = html +  '<th width=' + tdWidth[i] + '>' + names_G[i] + '</th>'
    }
    html = html + '</tr>'

    var s = (pageIndex -1) * showNumsPerPage
    var e = Math.min(dataLength, s + showNumsPerPage)
    for(i=s;i<e&&data[i];i++)
    {
      html = html + '<tr>'
      for(j=0;j<nameLength;j++)
      {
        html = html +  '<td>' + data[i][names_G[j]] + '</td>'
      }
      html = html + '</tr>'
    }
    html = html + 
    '</table>' + 
    '</body>'
  
    return html
}


exports.start = function(response, postData, query)
{

  var onGetMysqlData = function(data)
  {
    console.log("Request handler 'start' was called11.",query.pageIndex);
    query.pageIndex = query.pageIndex || 1
    query.pageIndex = parseInt(query.pageIndex)
    
    console.log("Request handler 'start' was called.",query.pageIndex);
    var body = getHtmlByJson(data, query.pageIndex)
    //response.writeHead(200,{"Content-Type":"text/html;charset=utf-8"});
    response.write(body);
    response.end();
  }

  getMysqlData(onGetMysqlData)

}

